package triseries;

import java.util.ArrayList;
import java.util.List;

import org.jdom.Element;


/**
 *  Copyright (C) 2011  Thomas ROBIN
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author tshepo
 * 
 */



public class UnwatchEpisode {
	
	private static final boolean DEBUG = (Main.debug || MyProperties.debug.equalsIgnoreCase("UnwatchEpisode"));
	private String url, show, title;
	private int  season, episode;
	private boolean dl;

	public UnwatchEpisode(Element e) {
		if (DEBUG)	System.err.println("UnwatchEpisode()");
		url = e.getChild("url").getText();
		show = e.getChild("show").getText();
		season = Integer.parseInt(e.getChild("season").getText());
		episode = Integer.parseInt(e.getChild("episode").getText());
		title = e.getChild("title").getText();
		dl = e.getChild("downloaded").getText().contentEquals("1");
	}
	
	public String GetShow() {
		return show;
	}

	public String GetSeason() {
		return String.format("%02d", season);
	}
	
	public int getSeason() {
		return season;
	}
	
	/**
	 * @return
	 */
	public String GetEpisode() {
		return String.format("%02d", episode);
	}
	public int getEpisode() {
		return episode;
	}
	
	/**
	 * @return
	 */
	public String GetUrl() {
		return url;
	}
	
	public boolean isPreviousOrSameEp(String url, int s, int ep) {
		if(!this.url.equalsIgnoreCase(url)) return false;
		if(s > season) return true;
		if(s < season) return false;
		if(ep < episode) return false;
		return true;
	}
	
	public MyMenuItem MakeItem() {
		if (DEBUG)	System.err.println("UnwatchEpisode.MakeItem() - "+GetShow()+GetSeason()+GetEpisode());
		MyMenuItem menuItem = new MyMenuItem(this);
		menuItem.setEnabled(dl);
		return menuItem;
	}
	


	public boolean sameShow(UnwatchEpisode UE) {
		return show.equalsIgnoreCase(UE.GetShow());
	}
	
	public boolean sameSeason(UnwatchEpisode UE) {
		return (season == UE.season);
	}
	
	public List<UnwatchEpisode> SameShow(List<UnwatchEpisode> list) {
		List<UnwatchEpisode> ret = new ArrayList<UnwatchEpisode>();
		for(UnwatchEpisode ue : list) {
			if(ue.sameShow(this))
				ret.add(ue);
		}
		return ret;
	}
	
	public List<UnwatchEpisode> SameSeason(List<UnwatchEpisode> list) {
		List<UnwatchEpisode> ret = new ArrayList<UnwatchEpisode>();
		for(UnwatchEpisode ue : list) {
			if(ue.sameSeason(this))
				ret.add(ue);
		}
		return ret;
	}

	/**
	 * @return
	 */
	public String GetTitle() {
		return title;
	}


	
}
